package com.lxyk.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxyk.pojo.Customer;
import com.lxyk.pojo.MainMenu;
import com.lxyk.pojo.Worker;
import com.lxyk.service.WorkerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/worker")
@Slf4j
public class WorkerController {
    @Autowired
    private WorkerService workerService;
    @Value("${location}")
    private String location;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询所有送水工  之后就用带分页的
     * @param model
     * @return
     */
    /*@RequestMapping("/workerList")
    public String listWorker(Model model){
        List<Worker> workerList = workerService.queryWorker();
        model.addAttribute("workerList",workerList);
        return "workerList";
    }*/

    /**
     * 查询所有带分页
     * @param pageNum
     * @param pageSize
     * @param model
     * @param worker
     * @return
     */
    @RequestMapping("/workerList")
    public String listCust(@RequestParam(required = false,defaultValue = "1",value = "pageNum")Integer pageNum,
                           @RequestParam(defaultValue = "10",value = "pageSize")Integer pageSize, Model model, Worker worker){

        if (pageNum <= 0 || pageNum.equals("") || pageNum == null){
            pageNum = 1;
        }
        if (pageSize <= 0 || pageSize.equals("") || pageSize == null){
            pageSize = 10;
        }
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Worker> pageInfo = workerService.listWorker(worker, pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "workerList";
    }

    /**
     * 页面跳转到添加页面
     * @return
     */
    @RequestMapping("/preSaveWorker")
    public String SaveWorker(){
        return "workerSave";
    }

    //难点来了 涉及到图片上传
    /**
     * 添加送水工
     * @param worker
     * @param file
     * @return
     */
    @PostMapping(value = "/workerSave" )
    public String workerSave(Worker worker, MultipartFile file){
        //思路： 添加信息 图片上传 姓名添加到redis
        transerFile(worker,file);
        workerService.saveWorker(worker);

        //拿到redis的长度 需要装配RedisTemplate
        Long size = redisTemplate.opsForList().size("mainMenu");
        if (size<12){
            MainMenu main=new MainMenu();
            main.setWorkerName(worker.getWorkerName());
            main.setSendWaterCount(0);
            redisTemplate.opsForList().rightPush("mainMenu",main);
        }

        //重定向到送水工页面
        return "redirect:/worker/workerList";
    }

    /**
     * 图片上传 添加和修改都用得到
     * @param worker
     * @param file
     */
    private void transerFile(Worker worker, MultipartFile file) {
        //判断 条件成立 上传了图片
        if (null!=file){
            //1.获取上传图片的名称
            String filename = file.getOriginalFilename();
            //2.获取文件后缀索引
            int index = filename.lastIndexOf(".");
            //3.获取后缀 .png
            String suffix = filename.substring(index);
            //4.前缀
            long prefix = System.nanoTime();
            //5.拼接  prefix + suffix
            String uploadFileName=prefix + suffix;
            //6.创建服务器存放浏览器上传文件的File对象
            /*//怎么取yml里的图片路径呢 ？ @Value注解
            * @Value("${location}")
              private String location;*/
            File uploadFile=new File(location);
            //7.条件成立 表示路径不存在 创建路径
            if (!uploadFile.exists()){
                uploadFile.mkdirs();
            }
            //创建上传文件的路径 参数一 文件路径 参数二 文件名称
            File path=new File(uploadFile,uploadFileName);
            try {
                file.transferTo(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
            worker.setWorkerImage(uploadFileName);

        }
    }

    /**
     * 页面跳转到修改页面
     * @return
     */
    @RequestMapping("/preUpdateWorker/{wid}")
    public String preUpdateWorker(@PathVariable("wid") Integer wid,Model model){
        Worker worker = workerService.getWorkerById(wid);
        model.addAttribute("worker",worker);
        return "workerUpdate";
    }

    /**
     * 修改送水工信息
     * @param worker
     * @param file
     * @return
     */
    @RequestMapping("/updateWorker")
    public String updateWorker(Worker worker,MultipartFile file){
        //调图片上传方法
        transerFile(worker,file);
        workerService.updateWorker(worker);
        return "redirect:/worker/workerList";
    }

    /**
     * 工资调整
     * @param wid
     * @param workerSalary
     * @return
     */
    //这里用到ajax页面局部刷新 好好学
    @PostMapping("/addSalary")
    @ResponseBody  //加上这个注解返回的就是json
    public String addSalary(Integer wid,Integer workerSalary){
        int rows = workerService.addSalary(wid, workerSalary);
        if (rows>0){
            return "OK";
        }else {
            return "error";
        }
    }
}
